package sdk.webview.fmc.com.fmcsdk.view;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import sdk.webview.fmc.com.fmcsdk.R;
import sdk.webview.fmc.com.fmcsdk.activity.CommonWebActivity;
import sdk.webview.fmc.com.fmcsdk.activity.LoginActivity;


public class FirstXiyiDialog extends Dialog {
    private Context context;

    private TextView confirm;
    private TextView tv_message;

    private ClickiInterface mClickiInterface;

    //只有提示内容的消息框
    public FirstXiyiDialog(Context context, ClickiInterface clickiInterface) {
        super(context, R.style.Dialog);
        this.context = context;
        this.mClickiInterface = clickiInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_xieyi_dialog);
        confirm = (TextView) findViewById(R.id.confirm);
        TextView cancel = (TextView) findViewById(R.id.cancel);
        tv_message = (TextView) findViewById(R.id.tv_message);
        confirm.setOnClickListener(new clickListener());
        cancel.setOnClickListener(new clickListener());
        //试着提示框的位置和大小
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高
        lp.width = (int) (d.widthPixels * 0.9);
        dialogWindow.setAttributes(lp);
        this.setCanceledOnTouchOutside(false);
        initMsg();

    }

    private void initMsg() {
        String clickableText = context.getString(R.string.xieyi_first_two);
        String fullText = context.getString(R.string.xieyi_first_one) + clickableText + context.getString(R.string.xieyi_first_three);


        SpannableString spannableString = new SpannableString(fullText);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                // 点击事件的处理逻辑
                Intent intent = new Intent(context, CommonWebActivity.class);
                context.startActivity(intent);
//                Toast.makeText(widget.getContext(), "链接被点击了", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(Color.BLUE); // 设置颜色
                ds.setUnderlineText(false); // 去掉下划线
            }
        };

        // 将ClickableSpan应用于SpannableString
        int startIndex = fullText.indexOf(clickableText);
        int endIndex = startIndex + clickableText.length();
        spannableString.setSpan(clickableSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // 启用TextView的文本点击
        tv_message.setMovementMethod(LinkMovementMethod.getInstance());
        tv_message.setText(spannableString);
    }

    private class clickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.confirm) {
                mClickiInterface.clickSure();
                dismiss();
            } else if (id == R.id.cancel) {
                mClickiInterface.clickCancel();
                dismiss();
            }

        }

    }


    public interface ClickiInterface {
        void clickSure();

        void clickCancel();
    }
}
